<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mockclassfactory.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_mockclassfactory.js.html">mockclassfactory.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file defines a factory that can be used to mock and
<a name="line17"></a> * replace an entire class.  This allows for mocks to be used effectively with
<a name="line18"></a> * &quot;new&quot; instead of having to inject all instances.  Essentially, a given class
<a name="line19"></a> * is replaced with a proxy to either a loose or strict mock.  Proxies locate
<a name="line20"></a> * the appropriate mock based on constructor arguments.
<a name="line21"></a> *
<a name="line22"></a> * The usage is:
<a name="line23"></a> * &lt;ul&gt;
<a name="line24"></a> *   &lt;li&gt;Create a mock with one of the provided methods with a specifc set of
<a name="line25"></a> *       constructor arguments
<a name="line26"></a> *   &lt;li&gt;Set expectations by calling methods on the mock object
<a name="line27"></a> *   &lt;li&gt;Call $replay() on the mock object
<a name="line28"></a> *   &lt;li&gt;Instantiate the object as normal
<a name="line29"></a> *   &lt;li&gt;Call $verify() to make sure that expectations were met
<a name="line30"></a> *   &lt;li&gt;Call reset on the factory to revert all classes back to their original
<a name="line31"></a> *       state
<a name="line32"></a> * &lt;/ul&gt;
<a name="line33"></a> *
<a name="line34"></a> * For examples, please see the unit test.
<a name="line35"></a> *
<a name="line36"></a> */
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>goog.provide(&#39;goog.testing.MockClassFactory&#39;);
<a name="line40"></a>goog.provide(&#39;goog.testing.MockClassRecord&#39;);
<a name="line41"></a>
<a name="line42"></a>goog.require(&#39;goog.array&#39;);
<a name="line43"></a>goog.require(&#39;goog.object&#39;);
<a name="line44"></a>goog.require(&#39;goog.testing.LooseMock&#39;);
<a name="line45"></a>goog.require(&#39;goog.testing.StrictMock&#39;);
<a name="line46"></a>goog.require(&#39;goog.testing.TestCase&#39;);
<a name="line47"></a>goog.require(&#39;goog.testing.mockmatchers&#39;);
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * A record that represents all the data associated with a mock replacement of
<a name="line53"></a> * a given class.
<a name="line54"></a> * @param {Object} namespace The namespace in which the mocked class resides.
<a name="line55"></a> * @param {string} className The name of the class within the namespace.
<a name="line56"></a> * @param {Function} originalClass The original class implementation before it
<a name="line57"></a> *     was replaced by a proxy.
<a name="line58"></a> * @param {Function} proxy The proxy that replaced the original class.
<a name="line59"></a> * @constructor
<a name="line60"></a> */
<a name="line61"></a>goog.testing.MockClassRecord = function(namespace, className, originalClass,
<a name="line62"></a>    proxy) {
<a name="line63"></a>  /**
<a name="line64"></a>   * A standard closure namespace (e.g. goog.foo.bar) that contains the mock
<a name="line65"></a>   * class referenced by this MockClassRecord.
<a name="line66"></a>   * @type {Object}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.namespace_ = namespace;
<a name="line70"></a>
<a name="line71"></a>  /**
<a name="line72"></a>   * The name of the class within the provided namespace.
<a name="line73"></a>   * @type {string}
<a name="line74"></a>   * @private
<a name="line75"></a>   */
<a name="line76"></a>  this.className_ = className;
<a name="line77"></a>
<a name="line78"></a>  /**
<a name="line79"></a>   * The original class implementation.
<a name="line80"></a>   * @type {Function}
<a name="line81"></a>   * @private
<a name="line82"></a>   */
<a name="line83"></a>  this.originalClass_ = originalClass;
<a name="line84"></a>
<a name="line85"></a>  /**
<a name="line86"></a>   * The proxy being used as a replacement for the original class.
<a name="line87"></a>   * @type {Function}
<a name="line88"></a>   * @private
<a name="line89"></a>   */
<a name="line90"></a>  this.proxy_ = proxy;
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * A mocks that will be constructed by their argument list.  The entries are
<a name="line94"></a>   * objects with the format {&#39;args&#39;: args, &#39;mock&#39;: mock}.
<a name="line95"></a>   * @type {Array.&lt;Object&gt;}
<a name="line96"></a>   * @private
<a name="line97"></a>   */
<a name="line98"></a>  this.instancesByArgs_ = [];
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * A mock associated with the static functions for a given class.
<a name="line104"></a> * @type {goog.testing.StrictMock|goog.testing.LooseMock|null}
<a name="line105"></a> * @private
<a name="line106"></a> */
<a name="line107"></a>goog.testing.MockClassRecord.prototype.staticMock_ = null;
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * A getter for this record&#39;s namespace.
<a name="line112"></a> * @return {Object} The namespace.
<a name="line113"></a> */
<a name="line114"></a>goog.testing.MockClassRecord.prototype.getNamespace = function() {
<a name="line115"></a>  return this.namespace_;
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * A getter for this record&#39;s class name.
<a name="line121"></a> * @return {string} The name of the class referenced by this record.
<a name="line122"></a> */
<a name="line123"></a>goog.testing.MockClassRecord.prototype.getClassName = function() {
<a name="line124"></a>  return this.className_;
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * A getter for the original class.
<a name="line130"></a> * @return {Function} The original class implementation before mocking.
<a name="line131"></a> */
<a name="line132"></a>goog.testing.MockClassRecord.prototype.getOriginalClass = function() {
<a name="line133"></a>  return this.originalClass_;
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * A getter for the proxy being used as a replacement for the original class.
<a name="line139"></a> * @return {Function} The proxy.
<a name="line140"></a> */
<a name="line141"></a>goog.testing.MockClassRecord.prototype.getProxy = function() {
<a name="line142"></a>  return this.proxy_;
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * A getter for the static mock.
<a name="line148"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock|null} The static
<a name="line149"></a> *     mock associated with this record.
<a name="line150"></a> */
<a name="line151"></a>goog.testing.MockClassRecord.prototype.getStaticMock = function() {
<a name="line152"></a>  return this.staticMock_;
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * A setter for the static mock.
<a name="line158"></a> * @param {goog.testing.StrictMock|goog.testing.LooseMock} staticMock A mock to
<a name="line159"></a> *     associate with the static functions for the referenced class.
<a name="line160"></a> */
<a name="line161"></a>goog.testing.MockClassRecord.prototype.setStaticMock = function(staticMock) {
<a name="line162"></a>  this.staticMock_ = staticMock;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Adds a new mock instance mapping.  The mapping connects a set of function
<a name="line168"></a> * arguments to a specific mock instance.
<a name="line169"></a> * @param {Array} args An array of function arguments.
<a name="line170"></a> * @param {goog.testing.StrictMock|goog.testing.LooseMock} mock A mock
<a name="line171"></a> *     associated with the supplied arguments.
<a name="line172"></a> */
<a name="line173"></a>goog.testing.MockClassRecord.prototype.addMockInstance = function(args, mock) {
<a name="line174"></a>  this.instancesByArgs_.push({args: args, mock: mock});
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Finds the mock corresponding to a given argument set.  Throws an error if
<a name="line180"></a> * there is no appropriate match found.
<a name="line181"></a> * @param {Array} args An array of function arguments.
<a name="line182"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock|null} The mock
<a name="line183"></a> *     corresponding to a given argument set.
<a name="line184"></a> */
<a name="line185"></a>goog.testing.MockClassRecord.prototype.findMockInstance = function(args) {
<a name="line186"></a>  for (var i = 0; i &lt; this.instancesByArgs_.length; i++) {
<a name="line187"></a>    var instanceArgs = this.instancesByArgs_[i].args;
<a name="line188"></a>    if (goog.testing.mockmatchers.flexibleArrayMatcher(instanceArgs, args)) {
<a name="line189"></a>      return this.instancesByArgs_[i].mock;
<a name="line190"></a>    }
<a name="line191"></a>  }
<a name="line192"></a>
<a name="line193"></a>  return null;
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Resets this record by reverting all the mocked classes back to the original
<a name="line199"></a> * implementation and clearing out the mock instance list.
<a name="line200"></a> */
<a name="line201"></a>goog.testing.MockClassRecord.prototype.reset = function() {
<a name="line202"></a>  this.namespace_[this.className_] = this.originalClass_;
<a name="line203"></a>  this.instancesByArgs_ = [];
<a name="line204"></a>};
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * A factory used to create new mock class instances.  It is able to generate
<a name="line210"></a> * both static and loose mocks.  The MockClassFactory is a singleton since it
<a name="line211"></a> * tracks the classes that have been mocked internally.
<a name="line212"></a> * @constructor
<a name="line213"></a> */
<a name="line214"></a>goog.testing.MockClassFactory = function() {
<a name="line215"></a>  if (goog.testing.MockClassFactory.instance_) {
<a name="line216"></a>    return goog.testing.MockClassFactory.instance_;
<a name="line217"></a>  }
<a name="line218"></a>
<a name="line219"></a>  /**
<a name="line220"></a>   * A map from class name -&gt; goog.testing.MockClassRecord.
<a name="line221"></a>   * @type {Object}
<a name="line222"></a>   * @private
<a name="line223"></a>   */
<a name="line224"></a>  this.mockClassRecords_ = {};
<a name="line225"></a>
<a name="line226"></a>  goog.testing.MockClassFactory.instance_ = this;
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * A singleton instance of the MockClassFactory.
<a name="line232"></a> * @type {goog.testing.MockClassFactory?}
<a name="line233"></a> * @private
<a name="line234"></a> */
<a name="line235"></a>goog.testing.MockClassFactory.instance_ = null;
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * The names of the fields that are defined on Object.prototype.
<a name="line240"></a> * @type {Array.&lt;string&gt;}
<a name="line241"></a> * @private
<a name="line242"></a> */
<a name="line243"></a>goog.testing.MockClassFactory.PROTOTYPE_FIELDS_ = [
<a name="line244"></a>  &#39;constructor&#39;,
<a name="line245"></a>  &#39;hasOwnProperty&#39;,
<a name="line246"></a>  &#39;isPrototypeOf&#39;,
<a name="line247"></a>  &#39;propertyIsEnumerable&#39;,
<a name="line248"></a>  &#39;toLocaleString&#39;,
<a name="line249"></a>  &#39;toString&#39;,
<a name="line250"></a>  &#39;valueOf&#39;
<a name="line251"></a>];
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Iterates through a namespace to find the name of a given class.  This is done
<a name="line256"></a> * solely to support compilation since string identifiers would break down.
<a name="line257"></a> * Tests usually aren&#39;t compiled, but the functionality is supported.
<a name="line258"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line259"></a> * @param {Function} classToMock The class whose name should be returned.
<a name="line260"></a> * @return {string} The name of the class.
<a name="line261"></a> * @private
<a name="line262"></a> */
<a name="line263"></a>goog.testing.MockClassFactory.prototype.getClassName_ = function(namespace,
<a name="line264"></a>    classToMock) {
<a name="line265"></a>  if (namespace === goog.global) {
<a name="line266"></a>    namespace = goog.testing.TestCase.getGlobals();
<a name="line267"></a>  }
<a name="line268"></a>  for (var prop in namespace) {
<a name="line269"></a>    if (namespace[prop] === classToMock) {
<a name="line270"></a>      return prop;
<a name="line271"></a>    }
<a name="line272"></a>  }
<a name="line273"></a>
<a name="line274"></a>  throw Error(&#39;Class is not a part of the given namespace&#39;);
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Returns whether or not a given class has been mocked.
<a name="line280"></a> * @param {string} className The name of the class.
<a name="line281"></a> * @return {boolean} Whether or not the given class name has a MockClassRecord.
<a name="line282"></a> * @private
<a name="line283"></a> */
<a name="line284"></a>goog.testing.MockClassFactory.prototype.classHasMock_ = function(className) {
<a name="line285"></a>  return !!this.mockClassRecords_[className];
<a name="line286"></a>};
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>/**
<a name="line290"></a> * Returns a proxy constructor closure.  Since this is a constructor, &quot;this&quot;
<a name="line291"></a> * refers to the local scope of the constructed object thus bind cannot be
<a name="line292"></a> * used.
<a name="line293"></a> * @param {string} className The name of the class.
<a name="line294"></a> * @param {Function} mockFinder A bound function that returns the mock
<a name="line295"></a> *     associated with a class given the constructor&#39;s argument list.
<a name="line296"></a> * @return {Function} A proxy constructor.
<a name="line297"></a> * @private
<a name="line298"></a> */
<a name="line299"></a>goog.testing.MockClassFactory.prototype.getProxyCtor_ = function(className,
<a name="line300"></a>    mockFinder) {
<a name="line301"></a>  return function() {
<a name="line302"></a>    this.$mock_ = mockFinder(className, arguments);
<a name="line303"></a>    if (!this.$mock_) {
<a name="line304"></a>      // The &quot;arguments&quot; variable is not a proper Array so it must be converted.
<a name="line305"></a>      var args = Array.prototype.slice.call(arguments, 0);
<a name="line306"></a>      throw Error(&#39;No mock found for &#39; + className + &#39; with arguments &#39; +
<a name="line307"></a>          args.join(&#39;, &#39;));
<a name="line308"></a>    }
<a name="line309"></a>  };
<a name="line310"></a>};
<a name="line311"></a>
<a name="line312"></a>
<a name="line313"></a>/**
<a name="line314"></a> * Returns a proxy function for a mock class instance.  This function cannot
<a name="line315"></a> * be used with bind since &quot;this&quot; must refer to the scope of the proxy
<a name="line316"></a> * constructor.
<a name="line317"></a> * @param {string} fnName The name of the function that should be proxied.
<a name="line318"></a> * @return {Function} A proxy function.
<a name="line319"></a> * @private
<a name="line320"></a> */
<a name="line321"></a>goog.testing.MockClassFactory.prototype.getProxyFunction_ = function(fnName) {
<a name="line322"></a>  return function() {
<a name="line323"></a>    return this.$mock_[fnName].apply(this.$mock_, arguments);
<a name="line324"></a>  };
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Find a mock instance for a given class name and argument list.
<a name="line330"></a> * @param {string} className The name of the class.
<a name="line331"></a> * @param {Array} args The argument list to match.
<a name="line332"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock} The mock found for
<a name="line333"></a> *     the given argument list.
<a name="line334"></a> * @private
<a name="line335"></a> */
<a name="line336"></a>goog.testing.MockClassFactory.prototype.findMockInstance_ = function(className,
<a name="line337"></a>    args) {
<a name="line338"></a>  return this.mockClassRecords_[className].findMockInstance(args);
<a name="line339"></a>};
<a name="line340"></a>
<a name="line341"></a>
<a name="line342"></a>/**
<a name="line343"></a> * Create a proxy class.  A proxy will pass functions to the mock for a class.
<a name="line344"></a> * The proxy class only covers prototype methods.  A static mock is not build
<a name="line345"></a> * simultaneously since it might be strict or loose.  The proxy class inherits
<a name="line346"></a> * from the target class in order to preserve instanceof checks.
<a name="line347"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line348"></a> * @param {Function} classToMock The class that will be proxied.
<a name="line349"></a> * @param {string} className The name of the class.
<a name="line350"></a> * @return {Function} The proxy for provided class.
<a name="line351"></a> * @private
<a name="line352"></a> */
<a name="line353"></a>goog.testing.MockClassFactory.prototype.createProxy_ = function(namespace,
<a name="line354"></a>    classToMock, className) {
<a name="line355"></a>  var proxy = this.getProxyCtor_(className,
<a name="line356"></a>      goog.bind(this.findMockInstance_, this));
<a name="line357"></a>  var protoToProxy = classToMock.prototype;
<a name="line358"></a>  goog.inherits(proxy, classToMock);
<a name="line359"></a>
<a name="line360"></a>  for (var prop in protoToProxy) {
<a name="line361"></a>    if (goog.isFunction(protoToProxy[prop])) {
<a name="line362"></a>      proxy.prototype[prop] = this.getProxyFunction_(prop);
<a name="line363"></a>    }
<a name="line364"></a>  }
<a name="line365"></a>
<a name="line366"></a>  // For IE the for-in-loop does not contain any properties that are not
<a name="line367"></a>  // enumerable on the prototype object (for example isPrototypeOf from
<a name="line368"></a>  // Object.prototype) and it will also not include &#39;replace&#39; on objects that
<a name="line369"></a>  // extend String and change &#39;replace&#39; (not that it is common for anyone to
<a name="line370"></a>  // extend anything except Object).
<a name="line371"></a>  // TODO (arv): Implement goog.object.getIterator and replace this loop.
<a name="line372"></a>
<a name="line373"></a>  goog.array.forEach(goog.testing.MockClassFactory.PROTOTYPE_FIELDS_,
<a name="line374"></a>      function(field) {
<a name="line375"></a>        if (Object.prototype.hasOwnProperty.call(protoToProxy, field)) {
<a name="line376"></a>          proxy.prototype[field] = this.getProxyFunction_(field);
<a name="line377"></a>        }
<a name="line378"></a>      }, this);
<a name="line379"></a>
<a name="line380"></a>  this.mockClassRecords_[className] = new goog.testing.MockClassRecord(
<a name="line381"></a>      namespace, className, classToMock, proxy);
<a name="line382"></a>  namespace[className] = proxy;
<a name="line383"></a>  return proxy;
<a name="line384"></a>};
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>/**
<a name="line388"></a> * Gets either a loose or strict mock for a given class based on a set of
<a name="line389"></a> * arguments.
<a name="line390"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line391"></a> * @param {Function} classToMock The class that will be mocked.
<a name="line392"></a> * @param {boolean} isStrict Whether or not the mock should be strict.
<a name="line393"></a> * @param {goog.array.ArrayLike} ctorArgs The arguments associated with this
<a name="line394"></a> *     instance&#39;s constructor.
<a name="line395"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock} The mock created
<a name="line396"></a> *     for the provided class.
<a name="line397"></a> * @private
<a name="line398"></a> */
<a name="line399"></a>goog.testing.MockClassFactory.prototype.getMockClass_ =
<a name="line400"></a>    function(namespace, classToMock, isStrict, ctorArgs) {
<a name="line401"></a>  var className = this.getClassName_(namespace, classToMock);
<a name="line402"></a>
<a name="line403"></a>  // The namespace and classToMock variables should be removed from the
<a name="line404"></a>  // passed in argument stack.
<a name="line405"></a>  ctorArgs = goog.array.slice(ctorArgs, 2);
<a name="line406"></a>
<a name="line407"></a>  if (goog.isFunction(classToMock)) {
<a name="line408"></a>    var mock = isStrict ? new goog.testing.StrictMock(classToMock) :
<a name="line409"></a>        new goog.testing.LooseMock(classToMock);
<a name="line410"></a>
<a name="line411"></a>    if (!this.classHasMock_(className)) {
<a name="line412"></a>      this.createProxy_(namespace, classToMock, className);
<a name="line413"></a>    } else {
<a name="line414"></a>      var instance = this.findMockInstance_(className, ctorArgs);
<a name="line415"></a>      if (instance) {
<a name="line416"></a>        throw Error(&#39;Mock instance already created for &#39; + className +
<a name="line417"></a>            &#39; with arguments &#39; + ctorArgs.join(&#39;, &#39;));
<a name="line418"></a>      }
<a name="line419"></a>    }
<a name="line420"></a>    this.mockClassRecords_[className].addMockInstance(ctorArgs, mock);
<a name="line421"></a>
<a name="line422"></a>    return mock;
<a name="line423"></a>  } else {
<a name="line424"></a>    throw Error(&#39;Cannot create a mock class for &#39; + className +
<a name="line425"></a>        &#39; of type &#39; + typeof classToMock);
<a name="line426"></a>  }
<a name="line427"></a>};
<a name="line428"></a>
<a name="line429"></a>
<a name="line430"></a>/**
<a name="line431"></a> * Gets a strict mock for a given class.
<a name="line432"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line433"></a> * @param {Function} classToMock The class that will be mocked.
<a name="line434"></a> * @param {...*} var_args The arguments associated with this instance&#39;s
<a name="line435"></a> *     constructor.
<a name="line436"></a> * @return {goog.testing.StrictMock} The mock created for the provided class.
<a name="line437"></a> */
<a name="line438"></a>goog.testing.MockClassFactory.prototype.getStrictMockClass =
<a name="line439"></a>    function(namespace, classToMock, var_args) {
<a name="line440"></a>  return /** @type {goog.testing.StrictMock} */ (this.getMockClass_(namespace,
<a name="line441"></a>      classToMock, true, arguments));
<a name="line442"></a>};
<a name="line443"></a>
<a name="line444"></a>
<a name="line445"></a>/**
<a name="line446"></a> * Gets a loose mock for a given class.
<a name="line447"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line448"></a> * @param {Function} classToMock The class that will be mocked.
<a name="line449"></a> * @param {...*} var_args The arguments associated with this instance&#39;s
<a name="line450"></a> *     constructor.
<a name="line451"></a> * @return {goog.testing.LooseMock} The mock created for the provided class.
<a name="line452"></a> */
<a name="line453"></a>goog.testing.MockClassFactory.prototype.getLooseMockClass =
<a name="line454"></a>    function(namespace, classToMock, var_args) {
<a name="line455"></a>  return /** @type {goog.testing.LooseMock} */ (this.getMockClass_(namespace,
<a name="line456"></a>      classToMock, false, arguments));
<a name="line457"></a>};
<a name="line458"></a>
<a name="line459"></a>
<a name="line460"></a>/**
<a name="line461"></a> * Creates either a loose or strict mock for the static functions of a given
<a name="line462"></a> * class.
<a name="line463"></a> * @param {Function} classToMock The class whose static functions will be
<a name="line464"></a> *     mocked.  This should be the original class and not the proxy.
<a name="line465"></a> * @param {string} className The name of the class.
<a name="line466"></a> * @param {Function} proxy The proxy that will replace the original class.
<a name="line467"></a> * @param {boolean} isStrict Whether or not the mock should be strict.
<a name="line468"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock} The mock created
<a name="line469"></a> *     for the static functions of the provided class.
<a name="line470"></a> * @private
<a name="line471"></a> */
<a name="line472"></a>goog.testing.MockClassFactory.prototype.createStaticMock_ =
<a name="line473"></a>    function(classToMock, className, proxy, isStrict) {
<a name="line474"></a>  var mock = isStrict ? new goog.testing.StrictMock(classToMock, true) :
<a name="line475"></a>      new goog.testing.LooseMock(classToMock, false, true);
<a name="line476"></a>
<a name="line477"></a>  for (var prop in classToMock) {
<a name="line478"></a>    if (goog.isFunction(classToMock[prop])) {
<a name="line479"></a>      proxy[prop] = goog.bind(mock.$mockMethod, mock, prop);
<a name="line480"></a>    } else if (classToMock[prop] !== classToMock.prototype) {
<a name="line481"></a>      proxy[prop] = classToMock[prop];
<a name="line482"></a>    }
<a name="line483"></a>  }
<a name="line484"></a>
<a name="line485"></a>  this.mockClassRecords_[className].setStaticMock(mock);
<a name="line486"></a>  return mock;
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * Gets either a loose or strict mock for the static functions of a given class.
<a name="line492"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line493"></a> * @param {Function} classToMock The class whose static functions will be
<a name="line494"></a> *     mocked.  This should be the original class and not the proxy.
<a name="line495"></a> * @param {boolean} isStrict Whether or not the mock should be strict.
<a name="line496"></a> * @return {goog.testing.StrictMock|goog.testing.LooseMock} The mock created
<a name="line497"></a> *     for the static functions of the provided class.
<a name="line498"></a> * @private
<a name="line499"></a> */
<a name="line500"></a>goog.testing.MockClassFactory.prototype.getStaticMock_ = function(namespace,
<a name="line501"></a>    classToMock, isStrict) {
<a name="line502"></a>  var className = this.getClassName_(namespace, classToMock);
<a name="line503"></a>
<a name="line504"></a>  if (goog.isFunction(classToMock)) {
<a name="line505"></a>    if (!this.classHasMock_(className)) {
<a name="line506"></a>      var proxy = this.createProxy_(namespace, classToMock, className);
<a name="line507"></a>      var mock = this.createStaticMock_(classToMock, className, proxy,
<a name="line508"></a>          isStrict);
<a name="line509"></a>      return mock;
<a name="line510"></a>    }
<a name="line511"></a>
<a name="line512"></a>    if (!this.mockClassRecords_[className].getStaticMock()) {
<a name="line513"></a>      var proxy = this.mockClassRecords_[className].getProxy();
<a name="line514"></a>      var originalClass = this.mockClassRecords_[className].getOriginalClass();
<a name="line515"></a>      var mock = this.createStaticMock_(originalClass, className, proxy,
<a name="line516"></a>          isStrict);
<a name="line517"></a>      return mock;
<a name="line518"></a>    } else {
<a name="line519"></a>      var mock = this.mockClassRecords_[className].getStaticMock();
<a name="line520"></a>      var mockIsStrict = mock instanceof goog.testing.StrictMock;
<a name="line521"></a>
<a name="line522"></a>      if (mockIsStrict != isStrict) {
<a name="line523"></a>        var mockType = mock instanceof goog.testing.StrictMock ? &#39;strict&#39; :
<a name="line524"></a>            &#39;loose&#39;;
<a name="line525"></a>        var requestedType = isStrict ? &#39;strict&#39; : &#39;loose&#39;;
<a name="line526"></a>        throw Error(&#39;Requested a &#39; + requestedType + &#39; static mock, but a &#39; +
<a name="line527"></a>            mockType + &#39; mock already exists.&#39;);
<a name="line528"></a>      }
<a name="line529"></a>
<a name="line530"></a>      return mock;
<a name="line531"></a>    }
<a name="line532"></a>  } else {
<a name="line533"></a>    throw Error(&#39;Cannot create a mock for the static functions of &#39; +
<a name="line534"></a>        className + &#39; of type &#39; + typeof classToMock);
<a name="line535"></a>  }
<a name="line536"></a>};
<a name="line537"></a>
<a name="line538"></a>
<a name="line539"></a>/**
<a name="line540"></a> * Gets a strict mock for the static functions of a given class.
<a name="line541"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line542"></a> * @param {Function} classToMock The class whose static functions will be
<a name="line543"></a> *     mocked.  This should be the original class and not the proxy.
<a name="line544"></a> * @return {goog.testing.StrictMock} The mock created for the static functions
<a name="line545"></a> *     of the provided class.
<a name="line546"></a> */
<a name="line547"></a>goog.testing.MockClassFactory.prototype.getStrictStaticMock =
<a name="line548"></a>    function(namespace, classToMock) {
<a name="line549"></a>  return /** @type {goog.testing.StrictMock} */ (this.getStaticMock_(namespace,
<a name="line550"></a>      classToMock, true));
<a name="line551"></a>};
<a name="line552"></a>
<a name="line553"></a>
<a name="line554"></a>/**
<a name="line555"></a> * Gets a loose mock for the static functions of a given class.
<a name="line556"></a> * @param {Object} namespace A javascript namespace (e.g. goog.testing).
<a name="line557"></a> * @param {Function} classToMock The class whose static functions will be
<a name="line558"></a> *     mocked.  This should be the original class and not the proxy.
<a name="line559"></a> * @return {goog.testing.LooseMock} The mock created for the static functions
<a name="line560"></a> *     of the provided class.
<a name="line561"></a> */
<a name="line562"></a>goog.testing.MockClassFactory.prototype.getLooseStaticMock =
<a name="line563"></a>    function(namespace, classToMock) {
<a name="line564"></a>  return /** @type {goog.testing.LooseMock} */ (this.getStaticMock_(namespace,
<a name="line565"></a>      classToMock, false));
<a name="line566"></a>};
<a name="line567"></a>
<a name="line568"></a>
<a name="line569"></a>/**
<a name="line570"></a> * Resests the factory by reverting all mocked classes to their original
<a name="line571"></a> * implementations and removing all MockClassRecords.
<a name="line572"></a> */
<a name="line573"></a>goog.testing.MockClassFactory.prototype.reset = function() {
<a name="line574"></a>  goog.object.forEach(this.mockClassRecords_, function(record) {
<a name="line575"></a>    record.reset();
<a name="line576"></a>  });
<a name="line577"></a>  this.mockClassRecords_ = {};
<a name="line578"></a>};
</pre>


</body>
</html>
